//
//  SelectServiceTypeCell.swift
//  koolcar
//
//  Created by xiantian on 2023/10/25.
//

import UIKit

class MaintenceServiceTypeCell: UITableViewCell {
    private var collectionView: UICollectionView!
    private var dataArray: [ServerProductContentModel] = []
    // private var selectIndex: NSInteger = 99999
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = Theme.viewBkgGrayColor
        
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize.init(width: 80, height: 100)
        layout.minimumInteritemSpacing = 10
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 100), collectionViewLayout: layout)
        self.collectionView.register(MaintenceServiceTypeItemCell.self, forCellWithReuseIdentifier: "MaintenceServiceTypeItemCell")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = Theme.viewBkgGrayColor
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 14, bottom: 0, right: 14)
        self.contentView.addSubview(self.collectionView)
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        self.collectionView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(0)
            make.width.equalTo(kScreenWidth)
            make.height.equalTo(1)
            make.bottom.equalTo(0)
        }
    }
    
    func setMaintenceServiceTypeCell(data: [ServerProductContentModel]) {
        self.dataArray = data
        if data.count > 0 {
            self.collectionView.snp.updateConstraints { make in
                make.top.equalTo(10)
                make.height.equalTo(100)
            }
            self.collectionView.reloadData()
        }else{
            self.collectionView.snp.updateConstraints { make in
                make.top.equalTo(0)
                make.height.equalTo(1)
            }
        }
        self.updateConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension MaintenceServiceTypeCell: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MaintenceServiceTypeItemCell", for: indexPath) as! MaintenceServiceTypeItemCell
        if self.dataArray.count > indexPath.row {
            let itemModel = self.dataArray[indexPath.row]
            cell.setMaintenceServiceTypeItemCell(data: itemModel)
         }
        cell.clipsToBounds = true
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if self.dataArray.count > indexPath.row {
            let itemModel = self.dataArray[indexPath.row]
            itemModel.selectFlag = !itemModel.selectFlag
            itemModel.selectProductBlock?()
            self.collectionView.reloadData()
        }
    }
}

